<template>
    <div class="table">
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item><i class="el-icon-tickets"></i> 收件箱</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-button type="primary" icon="delete" class="handle-del mr10" @click="delAll">批量删除</el-button>
            </div>
            <el-table :data="data" border style="width: 100%" ref="multipleTable" @selection-change="handleSelectionChange">
                <el-table-column type="selection" width="55"></el-table-column>
                <el-table-column prop="name" label="来信人" >
                </el-table-column>
                <el-table-column prop="time" label="来信时间" >
                    </el-table-column>
                <el-table-column prop="state" label="状态" >
                    <template slot-scope="scope">
                            <el-tag
                                :type="scope.row.state == '1' ? 'warning' : 'success'"
                                disable-transitions>{{scope.row.state == '1' ? '未读' : '已读'}}</el-tag>
                            </template>
                </el-table-column>
                <el-table-column label="操作" width="180">
                    <template slot-scope="scope">
                        <el-button size="small" @click="handleEdit(scope.$index, scope.row)">查看</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination @current-change="handleCurrentChange" layout="prev, pager, next"  :total="totalpage">
                </el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog :title="letter.name" :visible.sync="editVisible" width="40%">
            <el-form ref="letter" :model="letter" label-width="100px">
                <el-form-item label="来信人">
                    {{letter.name}}
                </el-form-item>
                <el-form-item label="时间">
                    {{letter.time}}
                </el-form-item>
                <el-form-item label="内容">
                    {{letter.content}}
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                    <el-button size="small" type="danger" @click="back(letter.sender_id,letter.id)">回信</el-button>
            </span>
        </el-dialog>
 <!-- 编辑弹出框 -->
 <el-dialog title="回信" :visible.sync="sendVisible" width="40%">
        <el-form ref="sender_letter" :model="sender_letter" label-width="100px">
            <el-form-item label="内容">
                <el-input v-model="sender_letter.content"  type="textarea" rows="10" ></el-input>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
                <el-button size="small" type="danger" @click="handleback(sender_letter)">回信</el-button>
        </span>
    </el-dialog>
        <!-- 删除提示框 -->
        <el-dialog title="提示" :visible.sync="delVisible" width="300px" center>
            <div class="del-dialog-cnt">删除不可恢复，是否确定删除？</div>
            <span slot="footer" class="dialog-footer">
                <el-button @click="delVisible = false">取 消</el-button>
                <el-button type="primary" @click="deleteRow">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
    export default {
        data() {
            return {
                url: '/api/getletter',
                tableData: [],
                cur_page: 1,
                multipleSelection: [],
                select_cate: '',
                select_word: '',
                del_list: [],
                asd:"<p>ssss</p>",
                totalpage:0,
                is_search: false,
                editVisible: false,
                delVisible: false,
                form: {
                 
                },
                sender_letter:{

                },
                sendVisible:false,
                letter:{
                name:"",
                send_id:"",
                content:""
                },
                idx: -1
            }
        },
        created() {
            this.getData();
        },
        computed: {
            data() {
                return this.tableData
            }
        },
        methods: {
            // 分页导航
            handleCurrentChange(val) {
                this.cur_page = val;
                this.getData();
            },
            // 获取 easy-mock 的模拟数据
            getData() {
                // 开发环境使用 easy-mock 数据，正式环境使用 json 文件
            
                this.$axios.post(this.url, {
                    page: this.cur_page
                }).then((res) => {
                    console.log(res.data);
                    this.tableData = res.data.data;
                    this.cur_page=res.data.current_page
                    this.totalpage=res.data.total
                    console.log(this.totalpage);
                })
              
                
            },
            back(id,letter_id){
                this.editVisible=false
                this.sendVisible=true
                this.sender_letter={
                    send_id:id,
                    letter_id:letter_id
                }
                console.log(this.sender_letter);
                
            },
            handleback(e){
                this.sendVisible=false
                console.log(this.sender_letter);
                this.$axios.post("/api/back_letter", {
                    page: this.cur_page,
                    send_id:this.sender_letter.send_id,
                    letter_id:this.sender_letter.letter_id,
                    content:this.sender_letter.content
                }).then((res) => {
                    console.log(res.data);
                    this.tableData = res.data.data;
                    this.cur_page=res.data.current_page,
                    this.totalpage=res.data.total
                })
                
            },
            handleEdit(index, row) {
                this.letter.sender_id=row.sender_id;
                this.letter.id=row.id
                this.letter.name=row.name;
                this.letter.time=row.time;
                this.letter.content=row.content;
                this.editVisible = true;
            },
            handleDelete(index, row) {
                this.idx = index;
                this.delVisible = true;
            },
            formatter(row, column) {
                return row.address;
            },
            delAll() {
                this.delVisible = false
                this.$axios.post("/api/badlist/del", {
                    page: this.cur_page,
                    id: this.multipleSelection
                }).then((res) => {
                    console.log(res.data);
                    this.tableData = res.data.data;
                    this.cur_page=res.data.current_page,
                    this.totalpage=res.data.total
                    this.$message.success('删除成功');
                })
                this.multipleSelection = [];
            },
            handleSelectionChange(val) {
                
                this.multipleSelection = [];
                val.forEach(element => {
                    this.multipleSelection.push(element.id)
                });
            },
          
            // 确定删除
            deleteRow(){
                this.delVisible = false
                this.$axios.post("/api/dish/del", {
                    page: this.cur_page,
                    id: this.tableData[this.idx].DishId
                }).then((res) => {
                    console.log(res.data);
                    this.tableData = res.data.data;
                    this.cur_page=res.data.current_page,
                    this.totalpage=res.data.total
                    this.$message.success('删除成功');
                })
               
            }
        }
    }

</script>

<style scoped>
    .handle-box {
        margin-bottom: 20px;
    }

    .handle-select {
        width: 120px;
    }

    .handle-input {
        width: 300px;
        display: inline-block;
    }
    .del-dialog-cnt{
        font-size: 16px;
        text-align: center
    }
</style>
